<?php

namespace app\admin\controller;

use app\model\AdminInfo as ModelAdminInfo;
use app\model\AdminGroup as ModelAdminGroup;

class Member extends Common
{
    protected $admin_id; // 管理员编号

    public function _initialize()
    {
        parent::_initialize();
        $obj = new \app\logic\Passport($this->str_module_name);
        $this->admin_id = $obj->_checkPassport($this->_getContentParams('passport'), $this->_getParams('platform'));
        unset($obj);
    }

    /**
     * 检查是否有权限执行
     * @param $str_number
     * @return bool
     */
    protected function checkIsPowerDo($str_number)
    {
        // 如果是超级管理员的话 直接返回成功
        if (1 == ModelAdminInfo::getInstance()->checkIsSuper($this->admin_id)) return true;

        $str_content = ModelAdminGroup::getInstance()->getContent($this->admin_id);
        if (strlen($str_content) > 0) {
            if (in_array($str_number, explode(',', $str_content))) {
                return true;
            }
        }

        _returnError(12053);
    }


    /**
     * 获取管理员账号的可使用菜单列表 \n
     * URI : /admin/get.menu.list
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 用户票据
     *
     * @return
     *  name      | type  | description
     * -----------|-------|-------------
     *  menu_id   | int   | 菜单编号
     *  menu_name | string| 菜单名称
     *  second    | array | 二级列表
     *
     * second:
     *  name      | type  | description
     * -----------|-------|-------------
     *  menu_id   | int   | 菜单编号
     *  menu_name | string| 菜单名称
     *
     */
    public function getAdminMenuList()
    {
        if (1 == ModelAdminInfo::getInstance()->checkIsSuper($this->admin_id)) {
            _returnData(config('admin_menu'));
        }

        $str_content = ModelAdminGroup::getInstance()->getContent($this->admin_id);

        if (!(strlen($str_content) > 0)) {
            _returnData([]);
        }

        $arr_param = [];
        foreach (explode(',', $str_content) as $value) {
            $arr_param[] = explode('_', $value)[0];
        }

        $arr_where = array_values(array_unique($arr_param));
        if (in_array(7, $arr_where) || in_array(8, $arr_where)) {
            $arr_where[] = '5';
        }

        $arr_data = config('admin_menu');
        $arr_new_data = [];
        $int_i = 0;

        foreach ($arr_data as $value) {
            if (in_array($value['menu_id'], $arr_where)) {
                $arr_new_data[$int_i] = $value;
                $int_i++;
            }
        }
        unset($value);

        _returnData($arr_new_data);
    }

    /**
     * 获取管理员账号可用的权限列表 \n
     * URI : /admin/get.do.authority.list
     * @param :
     *  name         | type   | null | description
     * --------------|--------|------|-------------
     *  passport     | string | 必填  | 用户票据
     *
     * @return
     *  name      | type   | description
     * -----------|--------|-------------
     *  number    | string | 权限编号
     *  menu_name | string | 菜单名称
     *  second    | array  | 二级列表
     *
     * second:
     *  name      | type   | description
     * -----------|--------|-------------
     *  number    | string | 权限编号
     *  menu_name | string | 菜单名称
     *
     */
    public function getDoAuthorityList()
    {
        if (1 == ModelAdminInfo::getInstance()->checkIsSuper($this->admin_id)) {
            _returnData(config('admin_list'));
        }

        $str_content = ModelAdminGroup::getInstance()->getContent($this->admin_id);

        if (!(strlen($str_content) > 0)) {
            _returnData([]);
        }

        $arr_where = explode(',', $str_content);
        $arr_data = config('admin_list');

        $arr_new_data = [];
        $int_i = 0;
        foreach ($arr_data as $value) {
            if (in_array($value['number'], $arr_where)) {
                $arr_new_data[$int_i]['number'] = $value['number'];
                $arr_new_data[$int_i]['menu_name'] = $value['menu_name'];
                if ($value['second'] == []) {
                    $arr_new_data[$int_i]['second'] = [];
                } else {
                    foreach ($value['second'] as $item) {
                        if (in_array($item['number'], $arr_where)) {
                            $arr_new_data[$int_i]['second'][] = $item;
                        }
                    }
                    unset($item);
                }
                $int_i++;
            }
        }

        _returnData($arr_new_data);
    }

    /**
     * 修改管理员登录密码 \n
     * URI : /admin/login.pwd.change
     * @param :
     *  name            | type   | null | description
     * -----------------|--------|------|-------------
     *  passport        | string | 必填 | 用户票据
     *  oldLoginPassword| string | 必填 | 旧登录密码
     *  newLoginPassword| string | 必填 | 新登录密码
     *
     * @return
     *  name    | type  | description
     * ---------|-------|-------------
     *  ------- | ----- | 无
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function changeLoginPassWord()
    {
        $str_old_login_password = $this->_getContentParams('oldLoginPassword');
        _toValidate('login_pwd', $str_old_login_password, 12013);

        $str_new_login_password = $this->_getContentParams('newLoginPassword');
        _toValidate('login_pwd', $str_new_login_password, 12014);

        $arr_merchant_info = ModelAdminInfo::getInstance()->toFind(['id' => $this->admin_id], 'login_password,pwd_key');

        // 验证登录密码是否正确
        _toCompare(passwordEncryption($str_old_login_password, $arr_merchant_info['pwd_key']), '!=', $arr_merchant_info['login_password'], 12013);

        $str_pwd_key = $this->_getRandomString(8);
        $arr_data = [
            'login_password' => passwordEncryption($str_new_login_password, $str_pwd_key),
            'pwd_key' => $str_pwd_key,
            'gmt_modified' => $this->_now,
        ];
        ModelAdminInfo::getInstance()->toUpdate(['id' => $this->admin_id], $arr_data);

        _returnData();
    }

    /**
     * 获取管理员信息 \n
     * URI : /admin/get.info
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填 | 用户票据
     *
     * @return
     *  name          | type   | description
     * ---------------|--------|-------------
     *  adminName     | string | 管理员名称
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAdminInfo()
    {
        $arr_admin_info = ModelAdminInfo::getInstance()->toFind(['id' => $this->admin_id], 'admin_name AS adminName');
        _returnData($arr_admin_info);
    }
}
